/**
 * Copyright (C) 2012 The SkyTvOS Project
 * <p/>
 * Version     Date           Author
 * ─────────────────────────────────────
 * 2014-6-24         luwei
 */

package com.tianci.appstore.ui.dialog;

import android.content.Context;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
import android.widget.ScrollView;
import android.widget.TextView;

import com.tianci.appstore.R;
import com.tianci.appstore.ui.data.SkyListAppData;
import com.tianci.appstore.ui.util.UICommon;
import com.tianci.appstore.ui.widget.ColorButton;
import com.tianci.appstore.utils.SkyConstants;

public class UpdateDialog extends CommonDialog {
    private ColorButton upgradeBtn;
    private ScrollView scroll;
    private TextView versionNameText;
    private TextView infoText;
    private final int UPGRADE_BTN_ID = 100;

    private int scrollOffset = 0;
    private int scrollStep = SkyConstants.Div(40);
    private int maxOffset;

    protected static int infoWidth = SkyConstants.Div(650);

    private String appId;

    public UpdateDialog(Context context) {
        super(context);
        initUI();
    }

    protected void initUI() {
        mainLayout.setPadding(SkyConstants.Div(39), SkyConstants.Div(30), 0, SkyConstants.Div(36));
        FrameLayout totalLayout = new FrameLayout(mContext);
        mainLayout.addView(totalLayout, new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));

        versionNameText = UICommon.newTextView(mContext, UICommon.dlgContentTextSize, R.color.versionNameColor);
        versionNameText.setPadding(0, 0, 0, SkyConstants.Div(6));
        infoText = UICommon.newTextView(mContext, UICommon.dlgContentTextSize, R.color.dlgInfoColor);
        infoText.setLineSpacing(SkyConstants.Div(12), 1.0f);
        infoText.setSingleLine(false);

        LinearLayout leftLayout = new LinearLayout(mContext);
        leftLayout.setOrientation(LinearLayout.VERTICAL);
        leftLayout.addView(versionNameText);
        leftLayout.addView(infoText);

        scroll = new ScrollView(mContext);
        scroll.setFocusable(false);
        scroll.setSmoothScrollingEnabled(true);
        scroll.addView(leftLayout, new LayoutParams(infoWidth, LayoutParams.WRAP_CONTENT));
        totalLayout.addView(scroll, new FrameLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));

        upgradeBtn = new ColorButton(mContext, R.string.upgrade, R.drawable.btn_green, R.drawable.btn_green_focus);
        upgradeBtn.setTag(0);
        upgradeBtn.setId(UPGRADE_BTN_ID);
        upgradeBtn.setNextFocusLeftId(UPGRADE_BTN_ID);
        upgradeBtn.setNextFocusUpId(UPGRADE_BTN_ID);
        upgradeBtn.setNextFocusRightId(UPGRADE_BTN_ID);
        upgradeBtn.setNextFocusDownId(UPGRADE_BTN_ID);
        upgradeBtn.setOnClickListener(this);
        upgradeBtn.setOnKeyListener(new View.OnKeyListener() {
            @Override
            public boolean onKey(View v, int keyCode, KeyEvent event) {
                // TODO Auto-generated method stub
                if (event.getAction() == KeyEvent.ACTION_UP)
                    return false;
                if (keyCode == KeyEvent.KEYCODE_DPAD_UP) {
                    if (scrollOffset > 0) {
                        scrollOffset -= scrollStep;
                        if (scrollOffset < 0)
                            scrollOffset = 0;
                        scroll.setScrollY(scrollOffset);
                    }
                } else if (keyCode == KeyEvent.KEYCODE_DPAD_DOWN) {
                    maxOffset = mainLayout.getHeight() - scroll.getHeight();
                    if (scrollOffset < maxOffset) {
                        scrollOffset += scrollStep;
                        if (scrollOffset > maxOffset)
                            scrollOffset = maxOffset;
                        scroll.setScrollY(scrollOffset);
                    }
                }
                return false;
            }
        });
        FrameLayout.LayoutParams btnParams = new FrameLayout.LayoutParams(
                UICommon.btnWidth, UICommon.btnHeight, Gravity.RIGHT);
        btnParams.rightMargin = SkyConstants.Div(39);
        totalLayout.addView(upgradeBtn, btnParams);

        upgradeBtn.requestFocus();
    }

    @Override
    public void setIconTitle(SkyListAppData data, String title) {
        // TODO Auto-generated method stub
        super.setIconTitle(data, title);
        this.appId = data.getId();
        upgradeBtn.requestFocus();
    }

    public void setContent(String versionName, String upgradeInfo) {
        versionNameText.setText(versionName);
        infoText.setText(upgradeInfo);
        if (infoText.getLineCount() > 7) {
            scroll.setLayoutParams(new FrameLayout.LayoutParams(
                    LayoutParams.MATCH_PARENT, SkyConstants.Div(372)));
            mainLayout.invalidate();
        }
    }

    public String getAppId() {
        return appId;
    }

    @Override
    protected void initContentSize() {
        // TODO Auto-generated method stub
        contentHeight = ViewGroup.LayoutParams.WRAP_CONTENT;
    }
}
